
import java.util.Random;
import java.util.concurrent.*;

public class CountDownLatchDemo {
    public static void main(String[] args) throws InterruptedException {
       CountDownLatch countDownLatch=new CountDownLatch(5);
        ExecutorService service= Executors.newFixedThreadPool(5);
        for (int i = 1; i < 6; i++) {
            service.submit(()->{
                Thread currThread=Thread.currentThread();
                System.out.println(currThread.getName()+"开始跑步");
                int runTime=(1+new Random().nextInt(5));
                try {
                    TimeUnit.SECONDS.sleep(runTime);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(currThread.getName()+"到达终点，用时："+runTime);
                countDownLatch.countDown();
            });
        }
        countDownLatch.await();
        System.out.println("宣布比赛结果");
    }
}
